// pages/order/order.js
var app = getApp();
var utils = require('../../utils/util.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        order_state: 0,
        currentPage:1,
        tishi_title: '正在加载中...',
    },

    /**
     * 生命周期函数--监听页面加载
     */
	onShow: function (options) {
        var that = this;
        that.getOrderList();
    },
    //tab切换
    changeOrderState: function (e) {
        var that = this;
        that.setData({
            order_state: e.currentTarget.dataset.order_state,
            currentPage: 1,
            orderList: [],
            tishi_title: '正在加载中...',
        })
        that.getOrderList()
    },
    getOrderList:function(){
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/order/my_order_list", {
            page: that.data.currentPage,
            order_state: that.data.order_state,
        }, function (res) {
            if (res.state == 1) {
                if(that.data.currentPage == 1){
                    that.setData({
                        orderList: []
                    })
                }
                // utils.hideLoading();
                var allpost = that.data.orderList;
                for (var i in res.datas) {
                    allpost.push(res.datas[i])
                }
                if (res.datas.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中...',
                    });
                }
                that.setData({
                    orderList: allpost,
                    ready: res.datas.length == 0 ? 1 : 2,
                })
                utils.hideLoading();
            } else {
                utils.hideLoading();
                utils.message(res.datas)
            }
        })
    },
	//进入店铺
	onStoreShopTap: function (e) {
			var that = this;
		utils.http("/api/v1/order/again_order", {
			order_id: e.currentTarget.dataset.id
			}, function (res) {
				if (res.state == 1) {
					wx.navigateTo({
						url: '../storeShop/storeShop?id=' + e.currentTarget.dataset.storeid
					})
				} else {
					utils.message(res.datas)
				}
			})
		
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    evaluationTap:function(e){
        wx.navigateTo({
			url: '../evaluation/evaluation?order_id=' + e.currentTarget.dataset.id + "&storeid=" + e.currentTarget.dataset.storeid
        })
    },
    //进入订单详情
    orderDetilTap:function(e){
        wx.navigateTo({
            url: '../orderDetil/orderDetil?order_id='+e.currentTarget.dataset.order_id
        })
    },
	onPhoneTap:function(e){

		wx.makePhoneCall({
			phoneNumber: e.currentTarget.dataset.username
		})
	},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
		var that = this;
		if (that.data.ready == 2) {
			var page = that.data.currentPage + 1
			that.setData({
				currentPage: page,
			})
			that.getOrderList();
		}
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})